package web;

import core.AjaxResult;
import core.BaseHttpServlet;
import dao.IMajorDao;
import dao.impl.MajorDaoImpl;
import domain.Major;
import domain.SignRecord;
import service.IMajorService;
import service.impl.MajorServiceImpl;
import util.RequestUtils;
import util.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 专业班级增删改查
 */
@WebServlet("/system/major_class/control")
public class MajorServlet extends BaseHttpServlet {
    private IMajorService majorService;
    @Override
    public void init() throws ServletException {
        majorService = new MajorServiceImpl(new MajorDaoImpl());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Major major = RequestUtils.getReqJsonBody(req, Major.class);
        try {
            int i = majorService.addMajor(major);
            ResponseUtils.responseJSON(resp, AjaxResult.success(i));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getLocalizedMessage());
            ResponseUtils.responseJSON(resp, AjaxResult.error(500, "添加失败，请联系管理员"));
        }
    }
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Major major = new Major();
        major.setId(Integer.parseInt(req.getParameter("id")));
        try {
            int i = majorService.delMajor(major);
            ResponseUtils.responseJSON(resp, AjaxResult.success(i));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getLocalizedMessage());
            ResponseUtils.responseJSON(resp, AjaxResult.error(500, "删除失败，请联系管理员"));
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Major major =RequestUtils.getReqJsonBody(req, Major.class);

    try {
            int i = majorService.updMajor(major);
            ResponseUtils.responseJSON(resp, AjaxResult.success(i));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getLocalizedMessage());
            ResponseUtils.responseJSON(resp, AjaxResult.error(500, "修改失败，请联系管理员"));
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Major major = new Major();
        major.setId(Integer.parseInt(req.getParameter("id")));
        try {
            List<Major> i = majorService.selMajor(major);
            ResponseUtils.responseJSON(resp, AjaxResult.success(i));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getLocalizedMessage());
            ResponseUtils.responseJSON(resp, AjaxResult.error(500, "查询失败，请联系管理员"));
        }
    }
}
